package com.cnap.multiclouldmgmt;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * <类文件描述>
 *
 * @author xc
 **/
public abstract class BaseModelTest<T> {
    protected abstract T getT();

    protected void testGetAndSet() throws InstantiationException, IllegalAccessException, IntrospectionException,
            InvocationTargetException {
        T t = getT();
        Class<?> cls = t.getClass();
        Object obj = cls.newInstance();
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            // 忽略jacoco添加的属性
            if (field.isSynthetic()) {
                continue;
            }
            // 忽略static字段
            int modifiers = field.getModifiers();
            if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
                continue;
            }

            PropertyDescriptor descriptor = new PropertyDescriptor((String)field.getName(), cls);
            Method get = descriptor.getReadMethod();
            Method set = descriptor.getWriteMethod();
            set.invoke(obj, get.invoke(obj));
        }
    }
}
